﻿using IRepositoryBll;
using Microsoft.AspNetCore.Mvc;
using RepositoryEntity;
using RepositoryEntity.Extension;
using RepositorySystem.Filter;
using RepositorySystem.Models;
using RepositorySystem.Models.WorkFlow_InstanceViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static RepositoryEntity.WorkFlow_InstanceStep;

namespace RepositorySystem.Controllers
{
    public class WorkFlow_InstanceController : Controller
    {
        private IWorkFlow_InstanceBll _workFlow_InstanceBll;
        private IWorkFlow_ModelBll _workFlow_ModelBll;
        private IConsumableInfoBll _consumableInfoBll;
        public WorkFlow_InstanceController(IWorkFlow_InstanceBll workFlow_InstanceBll , IWorkFlow_ModelBll workFlow_ModelBll , IConsumableInfoBll consumableInfoBll)
        {
            _workFlow_InstanceBll = workFlow_InstanceBll;
            _workFlow_ModelBll = workFlow_ModelBll;
            _consumableInfoBll = consumableInfoBll;
        }

        /// <summary>
        ///工作实例（申请流程）页面
        /// </summary>
        /// <returns></returns>
        [CustomActionFilter]
        public IActionResult WorkFlow_InstanceListView()
        {
            return View();
        }

        /// <summary>
        /// 添加工作实例（申请流程）页面
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        
        public IActionResult AddWorkFlow_InstanceView(AddWorkFlow_InstanceViewModels model)
        {
            return View();
        }

        /// <summary>
        /// 添加工作实例（申请流程）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddWorkFlow_Instance(AddWorkFlow_InstanceViewModels model)
        {
            CustomActionResult res = new CustomActionResult();
            UserInfo userInfo = this.GetSessionUserInfo();
            if (userInfo == null)
            {
                res.Msg = "未获取到登录用户的信息";
                return Json(res);
            }
            if (model.ModelId == null)
            {
                res.Msg = "模板标题不能为空";
                return Json(res);
            }
            if (string.IsNullOrEmpty(userInfo.DepartmentID))
            {
                res.Msg = "当前用户没加入任何部门";
                return Json(res);
            }
            WorkFlow_Instance workFlow_Instance = new WorkFlow_Instance()
            {

                ID = Guid.NewGuid().ToString(),
                ModelId = model.ModelId,
                Status = WorkFlow_Instance.InstanceStepStatus.审批中,
                Description = model.Description,
                Reason = model.Reason,
               
                Creator = userInfo.ID,
                OutNum = model.OutNum,
                OutGoodsId = model.OutGoodsId,
                IsDelete = false,
                CreateTime = DateTime.Now


            };
            bool isSuccess = _workFlow_InstanceBll.AddWorkFlow_Instance(workFlow_Instance , userInfo.DepartmentID , out string msg);
            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Msg = "成功";
                return Json(res);
            }
            else
            {
                return Json(res);
            }

        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public IActionResult GetWorkFlow_InstancelList(int page, int limit, string description)
        {
            int count;
            object workFlow_Instancel = _workFlow_InstanceBll.GetWorkFlow_InstancelListByPage(page, limit, out count, description);

            return Json(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = workFlow_Instancel
            });
        }

        /// <summary>
        /// 获取下拉选数据
        /// </summary>
        /// <returns></returns>
        public IActionResult GetSelectOption()
        {
            CustomActionResult res = new CustomActionResult();

            //获取流程模板下拉选数据
            object workFlow_ModelOptions = _workFlow_ModelBll.GetWorkFlow_ModelSelectOption();
            //获取物品名字下拉选数据
            object consumableInfoOptions = _consumableInfoBll.GetConsumableInfoSelectOption();
            res.IsSuccess = true;
            res.Msg = "成功";
            res.Status = 1;
            res.Data = new
            {
                ConsumableInfoOptions = consumableInfoOptions,
                WorkFlow_ModelOptions = workFlow_ModelOptions
            };
            return Json(res);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="WorkFlow_InstanceId"></param>
        /// <returns></returns>
        public IActionResult DeleteWorkFlow_Instance(string workFlow_InstanceId)
        {
            CustomActionResult res = new CustomActionResult();
            if (_workFlow_InstanceBll.Delete(workFlow_InstanceId))
            {
                res.Status = 1;
                res.Msg = "成功";
                res.IsSuccess = true;
                return Json(res);
            }
            else
            {
                return Json(res);
            }
        }

        /// <summary>
        /// 作废流程实例
        /// </summary>
        /// <param name="workFlow_instanceId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult CancelWorkFlow_Instance(string workFlow_instanceId)
        {
            CustomActionResult res = new CustomActionResult();
            UserInfo userInfo = this.GetSessionUserInfo();
            if (userInfo == null)
            {
                res.Msg = "未获取到登录用户的信息";
                return Json(res);
            }

            if (_workFlow_InstanceBll.CancelWorkFlow_Instance(workFlow_instanceId, userInfo))
            {
                res.Status = 1;
                res.Msg = "成功";
                res.IsSuccess = true;
                return Json(res);
            }
            else
            {
                return Json(res);
            }
        }
    }
}
